package com.mayikt.servlet;

import com.mayikt.entity.FlightEntity;
import com.mayikt.service.FlightService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * @author 余胜军
 * @ClassName UpdateFlightServlet
 * @qq 644064779
 * @addres www.mayikt.com
 * 微信:yushengjun644
 */
@WebServlet("/updateFlight")
public class UpdateFlightServlet extends HttpServlet {
    private FlightService flightService = new FlightService();

    /**
     * doGet 用户点击修改该条数据-------需要查询到该条数据（回显）
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取需要修改的id值
        String idStr = req.getParameter("id");
        if (idStr == null || idStr == "") {
            // 跳转到错误页面 客户端传递参数有问题
            /// 转发到错误jsp中
            req.setAttribute("errorMsg", "id的值是为空!");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }
        try {
            // mayikt1
            Integer id = Integer.parseInt(idStr);
            FlightEntity flightEntity = flightService.getByIdFlight(id);
            if (flightEntity == null) {
                /// 转发到错误jsp中
                req.setAttribute("errorMsg", "该id在db中无法查询到数据");
                req.getRequestDispatcher("error.jsp").forward(req, resp);
                return;
            }
            //转发到修改航班信息页面
            req.setAttribute("flight", flightEntity);
            req.getRequestDispatcher("updateFlight.jsp").forward(req, resp);
        } catch (Exception e) {
            req.setAttribute("errorMsg", "系统错误!");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
        }
    }

    /**
     * 接受用户需要修改的数据
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
